package com.framework.Module.base.meet;

import com.framerock.baselib.CDate;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.Flow.FlowListener;

import java.util.Map;

public class FlowListenermeet extends FlowListener {

    protected long nowlong;

    @Override
    public void oninitFlow() {
        this.nowlong = Long.parseLong(CDate.gettime());
    }

    @Override
    public Map<String, String> onReplacers(Map<String, String> da, int lx) {
        int state    = Rock.getMapint(da, "state");
        int newstate = Rock.getMapint(da, "state");

        if(state != Tablemeet.STATE_CANCEL && state != Tablemeet.STATE_END){
            newstate = this.getnewState(da);
        }

        if(newstate != state){
            da.put("state", newstate+"");
            mFlow.mModel.update("`state`="+newstate+"", da.get("id"));
        }

        return da;
    }

    /**
     * 获取新的状态
     * */
    private int getnewState(Map<String, String> da)
    {
        int newstate = 0;
        long startlong = Long.parseLong(CDate.now("time", da.get("startdt")));
        long endlong   = Long.parseLong(CDate.now("time", da.get("enddt")));
        if(this.nowlong >= startlong && endlong >= this.nowlong)newstate = Tablemeet.STATE_ING;
        if(this.nowlong > endlong)newstate = Tablemeet.STATE_END;
        return newstate;
    }

    @Override
    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        int state = this.getnewState(da);
        mFlow.mModel.update("`state`="+state+"", mid);
    }


    public void onFlowunitadd() {
        //添加时让首页显示
        mFlow.updateUnit(Rock.getMap("issx=1,color=#fc6419"), mFlow.funitid, "");
    }

    @Override
    public String ongetFlowwhere(String num) {
        String where = "";
        if(num.equals("mytoday")){
            String dts = CDate.date();
            where = "`startdt` <= '"+dts+" 23:59:59' AND `enddt`>='"+dts+" 00:00:00' ";
        }
        return where;
    }


    @Override
    public Map<String, String> onhomeData(String pnum) {
        if(Rock.isEmpt(pnum)) {
            String dt = CDate.date();
            Map<String, String> da = Rock.getMap();
            da.put("data", Json.getListString(mFlow.getListData("mytoday", "*", 5, true)));
            da.put("date", dt);
            return da;
        }else{
            return null;
        }
    }
}
